<?php
namespace app\article\controller;
use think\Request;
use app\common\controller\Base;

class ArticleDetail extends Base
{
    protected $beforeActionList = ['start'];
    public function start() {
        $this->itemType = 'article';
        $this->itemName = '内容';
        $this->item = 'Articles';
        $this->itemCategory = 'ArticlesCategory';
        $this->itemContent = 'ArticlesContent';
        $this->itemSearch = 'ArticlesSearch';
        $this->itemSearchRel = 'ArticlesSearchRel';
        $this->itemTagsRel = 'ArticlesTagsRel';
        $this->itemModelNameSpace = 'app\article\model\Articles';
        $this->itemCategoryModelNameSpace = 'app\article\model\ArticlesCategory';
        $this->itemTagsRelModelNameSpace = 'app\article\model\ArticlesTagsRel';
        $this->itemSettingInfo = config('articleSettingInfo');
    }
    
    public function index()
    {
        $id = input('param.id');
        $cid = input('param.cid');
        $page = input('param.page');
        if ($id) {
            $itemId = $this->itemSettingInfo['uuidStatus'] ? 'uuid' : 'id';
            $itemInfo = db($this->item)->where($itemId,$id)->find();
        }
        if ((!$id) || !$itemInfo) {
            throw new \think\exception\HttpException(404, '访问的内容不存在');
        }
        
        $itemInfo = model($this->itemModelNameSpace)->getItemInfo($itemInfo['id']);
        
        model($this->itemModelNameSpace)->updateViews($itemInfo['id']);
        
        $itemInfo = model($this->itemModelNameSpace)->getContentByItemInfo($itemInfo);
	     
        if (config('addonsInfo')['isFilterLink']) {
	        $itemInfo['content'] = filterLink($itemInfo['content']);
    	}
        
        $this->assign('categoryUrlName',$itemInfo['categoryInfo']['url_name']);
        $this->assign('itemDetailId',$itemInfo['uuid']);
        $this->assign('page',$page ? $page : 1);
        $this->assign('itemInfo',$itemInfo);
        
        $siteTitle = $itemInfo['seo_title'] ? $itemInfo['seo_title'] : $itemInfo['title'] . $pageText . $this->settingInfo['titleSeparator'] . $this->settingInfo['siteName'];
        $this->assign('siteTitle', $siteTitle);
        $this->assign('siteKeywords',$itemInfo['keywords']);
        $this->assign('siteDescription',$itemInfo['description']);
        
        $templateName = $itemInfo['categoryInfo']['detail_template'] ? $itemInfo['categoryInfo']['detail_template'] : 'articleDetail';
        $templateName = str_replace('.html', '', $templateName);
        $itemInfo['categoryInfo']['model_name'] = $itemInfo['categoryInfo']['model_name'] ? $itemInfo['categoryInfo']['model_name'] : 'article';
        return $this->siteView($itemInfo['categoryInfo']['model_name'] . '/' . $templateName);
        
    }
    
}